/* esp32 cam 小车
后排
P2  -> IN1
P4  -> IN2
P3  -> IN4
P1  -> IN3

前排
P12  -> IN4
P13  -> IN3
P15  -> IN2
P14  -> IN1

*/
#define BLINKER_WIFI
#define BLINKER_ESP_SMARTCONFIG
#include <WiFi.h>
#include <Blinker.h>
#include "ESP32_CAM_SERVER.h"

char auth[] = "be031a9ef438";
char ssid[] = "zhang2";
char pswd[] = "zrz15172442270";
bool setup_camera = false;




/**------------------- GPIO 管脚-------------------------**/
#define LED 4
#define LED2 33

//一号电机
const int topLeftPin=14;
const int topLeftPin2=15;
//二号电机
const int topRightPin=12;
const int topRightPin2=13;

//三号电机
const int bottomLeftPin=2;
//原本想要使用16或者0号端口的，但是发现这两个IO口却被占用了，无奈只能使用摄像头的闪光灯，
//还有一种方案，飞线使用33口，但是麻烦，就没用
const int bottomLeftPin2=4;

//四号电机
const int bottomRightPin=1;
const int bottomRightPin2=3;


#define Left_btn_key "left"
#define Right_btn_key "right"
#define Top_btn_key "top"
#define Bottom_btn_key "bottom"
#define led_btn_key "led"

BlinkerButton leftBtn(Left_btn_key);
BlinkerButton rightBtn(Right_btn_key);

BlinkerButton topBtn(Top_btn_key);
BlinkerButton bottomBtn(Bottom_btn_key);

BlinkerButton ledBtn(led_btn_key);

void ledBlink(){
  digitalWrite(LED, !digitalRead(LED));
  delay(100);
  digitalWrite(LED, !digitalRead(LED));
}
void ledBlink2(){
  digitalWrite(LED2, !digitalRead(LED2));
  delay(100);
  digitalWrite(LED2, !digitalRead(LED2));
}

void check(String name){
  ledBlink2();
  if(name=="left"){
    digitalWrite(topLeftPin, HIGH);
    digitalWrite(topLeftPin2, LOW);
    digitalWrite(topRightPin, LOW);
    digitalWrite(topRightPin2, HIGH);
    digitalWrite(bottomLeftPin, HIGH);
    digitalWrite(bottomLeftPin2, LOW);
    digitalWrite(bottomRightPin, LOW);
    digitalWrite(bottomRightPin2, HIGH);
    
  }else if(name=="right"){
    digitalWrite(topLeftPin, LOW);
    digitalWrite(topLeftPin2, HIGH);
    digitalWrite(topRightPin, HIGH);
    digitalWrite(topRightPin2, LOW);
    digitalWrite(bottomLeftPin, LOW);
    digitalWrite(bottomLeftPin2, HIGH);
    digitalWrite(bottomRightPin, HIGH);
    digitalWrite(bottomRightPin2, LOW);
  }
  else if(name=="bottom"){
    digitalWrite(topLeftPin, LOW);
    digitalWrite(topLeftPin2, HIGH);
    digitalWrite(topRightPin, LOW);
    digitalWrite(topRightPin2, HIGH);
    digitalWrite(bottomLeftPin, LOW);
    digitalWrite(bottomLeftPin2, HIGH);
    digitalWrite(bottomRightPin, LOW);
    digitalWrite(bottomRightPin2, HIGH);
  }
  else if(name=="top"){
    digitalWrite(topLeftPin, HIGH);
    digitalWrite(topLeftPin2, LOW);
    digitalWrite(topRightPin, HIGH);
    digitalWrite(topRightPin2, LOW);
    digitalWrite(bottomLeftPin, HIGH);
    digitalWrite(bottomLeftPin2, LOW);
    digitalWrite(bottomRightPin, HIGH);
    digitalWrite(bottomRightPin2, LOW);
  }else{
    //按钮释放,全部设置为低电位
    digitalWrite(topLeftPin, LOW);
    digitalWrite(topLeftPin2, LOW);
    digitalWrite(topRightPin, LOW);
    digitalWrite(topRightPin2, LOW);
    digitalWrite(bottomLeftPin, LOW);
    digitalWrite(bottomLeftPin2, LOW);
    digitalWrite(bottomRightPin, LOW);
    digitalWrite(bottomRightPin2, LOW);

  }
}


void left_callback(const String & state){
  //BLINKER_LOG("left_callback: ", state,values);
  
  //按钮按下
  leftBtn.print(state);
  if(state=="press"){
    check("left");
  }else if(state=="pressup"){
    check("1");
  }
}

void right_callback(const String & state){
  //BLINKER_LOG("right_callback: ", state,values);
  //按钮按下
  rightBtn.print(state);
  if(state=="press"){
    check("right");
  }else if(state=="pressup"){
    //按钮释放,全部设置为低电位
    check("1");
  }
  
  
}

void bottom_callback(const String & state){
  //BLINKER_LOG("right_callback: ", state,values);
  //按钮按下
  bottomBtn.print(state);
  if(state=="press"){
    check("bottom");
  }else if(state=="pressup"){
    //按钮释放,全部设置为低电位
   check("1");
  }
  
  
}


void top_callback(const String & state){
  //BLINKER_LOG("right_callback: ", state,values);
  topBtn.print(state);
  //按钮按下
  if(state=="press"){
     check("top");
  }else if(state=="pressup"){
    //按钮释放,全部设置为低电位
    check("1");
  }
  
  
}

void led_callback(const String & state){
  //BLINKER_LOG("led_callback: ", state);
  digitalWrite(LED, !digitalRead(LED));
  leftBtn.print("on");
}



void dataRead(const String & data)
{
    //BLINKER_LOG("Blinker readString: ", data);

    Blinker.vibrate();
    
    uint32_t BlinkerTime = millis();
    
    Blinker.print("millis", BlinkerTime);
}
void setup()
{
    //Serial.begin(115200);
    //BLINKER_DEBUG.stream(Serial);

    pinMode(LED, OUTPUT);
    digitalWrite(LED, LOW);
    //电源指示灯
    pinMode(LED2, OUTPUT);
    digitalWrite(LED2, LOW);

      //设置初始化GPIO
     pinMode(topLeftPin, OUTPUT);
     digitalWrite(topLeftPin, LOW);
     pinMode(topLeftPin2, OUTPUT);
     digitalWrite(topLeftPin2, LOW);

     pinMode(topRightPin, OUTPUT);
     digitalWrite(topRightPin, LOW);
     pinMode(topRightPin2, OUTPUT);
     digitalWrite(topRightPin2, LOW);
     
     pinMode(bottomLeftPin, OUTPUT);
     digitalWrite(bottomLeftPin, LOW);
     pinMode(bottomLeftPin2, OUTPUT);
     digitalWrite(bottomLeftPin2, LOW);

     pinMode(bottomRightPin, OUTPUT);
     digitalWrite(bottomRightPin, LOW);
     pinMode(bottomRightPin2, OUTPUT);
     digitalWrite(bottomRightPin2, LOW);

    WiFi.begin(ssid, pswd);
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
    }
    Blinker.begin(auth);
    Blinker.attachData(dataRead);
    ledBlink();
    ledBlink2();
    
    leftBtn.attach(left_callback);
    rightBtn.attach(right_callback);
    
    topBtn.attach(top_callback);
    bottomBtn.attach(bottom_callback);
    
    ledBtn.attach(led_callback);
}

void loop()
{
    Blinker.run();
    
    if (Blinker.connected() && !setup_camera)
    {
        setupCamera();
        setup_camera = true;
        Blinker.printObject("video", "{\"str\":\"mjpg\",\"url\":\"http://"+ WiFi.localIP().toString() + "\"}");
    }
    
}
